/*
 * j4Win
 *  http://code.google.com/p/j4win/
 *
 * Copyright (C) 2006-2007 Felix Geyer, Moritz Laschinger
 *
 * This file is part of j4Win.
 *
 * j4Win is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * j4Win is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package j4win;

import java.awt.*;
import java.awt.event.*;

public class NewGame extends Dialog {
	private static final long serialVersionUID = 1L;
	
	private Panel modePanel = new Panel();
	private Choice mode = new Choice();
	
	private Panel details = new Panel();
	
	private Panel local = new Panel();
	private Choice localPlayer1 = new Choice();
	private Choice localPlayer2 = new Choice();
	
	private Panel client = new Panel();
	private Choice clientPlayer = new Choice();
	private TextField clientIP = new TextField(10);
	private TextField clientPort = new TextField("4083", 5); //$NON-NLS-1$
	
	private Panel server = new Panel();
	private Choice serverPlayer = new Choice();
	private TextField serverPort = new TextField("4083", 5); //$NON-NLS-1$
	
	private boolean isOk = false;
	private Panel quit = new Panel();
	private Button ok = new Button(Messages.getString("NewGame.ok")); //$NON-NLS-1$
	private Button cancel = new Button(Messages.getString("NewGame.cancel")); //$NON-NLS-1$
	
	private String[] players = {
		Messages.getString("NewGame.human"), "KIMinMax (6)", "KIMinMax (8)", "KIMinMax (10)", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		"KISimple", "KICrazyBug", "KIRandom" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	};
	
	public NewGame(Frame owner) {
		super(owner, Messages.getString("NewGame.new"), true); //$NON-NLS-1$
		
		setResizable(false);
		setSize(300, 150);
		Point ol = owner.getLocation();
		setLocation(
			ol.x + (owner.getWidth()-getWidth())/2,
			ol.y + (owner.getHeight()-getHeight())/2
		);
		
		for (int i=0; i<players.length; ++i) {
			localPlayer1.add(players[i]);
			localPlayer2.add(players[i]);
			clientPlayer.add(players[i]);
			serverPlayer.add(players[i]);
		}
		
		setLayout(new GridLayout(3, 1));
		
		local.setLayout(new FlowLayout());
		local.add(localPlayer1);
		local.add(localPlayer2);
		
		client.setLayout(new FlowLayout());
		client.add(clientPlayer);
		client.add(clientIP);
		client.add(clientPort);
		
		server.setLayout(new FlowLayout());
		server.add(serverPlayer);
		server.add(serverPort);
		
		mode.add(Messages.getString("NewGame.local")); //$NON-NLS-1$
		mode.add(Messages.getString("NewGame.client")); //$NON-NLS-1$
		mode.add(Messages.getString("NewGame.server")); //$NON-NLS-1$
		mode.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				details.remove(local);
				details.remove(client);
				details.remove(server);
				
				switch (mode.getSelectedIndex()) {
					case 0:
						details.add(local);
						break;
					case 1:
						details.add(client);
						break;
					case 2:
						details.add(server);
						break;
				}
				
				validate();
			}
		});
		modePanel.setLayout(new FlowLayout());
		modePanel.add(mode);
		add(modePanel);
		
		details.setLayout(new FlowLayout());
		details.add(local);
		add(details);
		
		ok.addActionListener(actionQuit);
		cancel.addActionListener(actionQuit);
		quit.setLayout(new FlowLayout());
		quit.add(ok);
		quit.add(cancel);
		add(quit);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				actionQuit.actionPerformed(new ActionEvent(cancel, 0, "")); //$NON-NLS-1$
			}
		});
	}
	
	private ActionListener actionQuit = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			isOk = (e.getSource()==ok);
			setVisible(false);
		}
	};
	
	public boolean isOk() {
		return isOk;
	}
	
	private KI getKI(int index) {
		switch (index) {
			case 0:
				return null;
			case 1:
				return new KIMinMax(6);
			case 2:
				return new KIMinMax(8);
			case 3:
				return new KIMinMax(10);
			case 4:
				return new KISimple();
			case 5:
				return new KICrazyBug();
			case 6:
				return new KIRandom();
			default:
				throw new RuntimeException("Invald index"); //$NON-NLS-1$
		}
	}
	
	public Game getGame(GUI gui) {
		if (mode.getSelectedIndex()==0) {
			return new GameServer(
				gui,
				(localPlayer1.getSelectedIndex()==0) ? Game.PlayerType.Local : Game.PlayerType.KI,
				(localPlayer2.getSelectedIndex()==0) ? Game.PlayerType.Local : Game.PlayerType.KI,
				new KI[] {getKI(localPlayer1.getSelectedIndex()), getKI(localPlayer2.getSelectedIndex())}
			);
		}
		else if (mode.getSelectedIndex()==1) {
			return new GameClient(
				gui,
				(clientPlayer.getSelectedIndex()==0) ? Game.PlayerType.Local : Game.PlayerType.KI,
				Game.PlayerType.NetworkServer,
				new KI[] {getKI(clientPlayer.getSelectedIndex()), null},
				clientIP.getText(),
				Integer.parseInt(clientPort.getText())
			);
		}
		else {
			return new GameServer(
				gui,
				(serverPlayer.getSelectedIndex()==0) ? Game.PlayerType.Local : Game.PlayerType.KI,
				Game.PlayerType.NetworkClient,
				new KI[] {getKI(serverPlayer.getSelectedIndex()), null},
				Integer.parseInt(serverPort.getText())
			);
		}
	}
}
